﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using XLua;
using System;
//using static StarkSDKSpace.CanIUse.StarkAdManager;
using TTSDK;

/// <summary>
/// 主菜单
/// Create:2018/10
/// Last Edit:2019/1/3
/// </summary>
public class DemoMenu : MonoBehaviour {

    public Text buildVersion;

    //internal static LuaEnv luaEnv = new LuaEnv();
    private float GCInterval = 1;
    //private LuaTable scriptTable;
    private Action setVersion;

    private string tableScreenAdid = "15ibj2j7nd0l131c6c";
    static bool isFirstEnter = true;
    TTInterstitialAd interstitialAd;

    //[LuaCallCSharp]
    private void Start()
    {
        //scriptTable = luaEnv.NewTable();

        //LuaTable mate = luaEnv.NewTable();
        //mate.Set("_Index", luaEnv.Global);
        //scriptTable.SetMetaTable(mate);
        //mate.Dispose();

        //scriptTable.Set("self",this);
        //luaEnv.DoString("require 'DemoMenu'");
        //luaEnv.Global.Set("self", this);
        //luaEnv.Global.Get("setversion", out setVersion);

        //if (setVersion != null)
        //{
        //    setVersion();
        //}

        if (isFirstEnter)
        {
            StartCoroutine(OpenInterstitialAd());
        }
    }

    private void Update()
    {
        //if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
        //{
        //    luaEnv.Tick();
        //    LuaBehaviour.lastGCTime = Time.time;
        //}
    }

    private void OnDestroy()
    {
        //scriptTable.Dispose();
    }

    /// <summary>
    /// 开始游戏
    /// </summary>
	public void StartGame()
	{
		SceneManager.Instance.OpenMainScene ();
	}

    /// <summary>
    /// 开始教学模式
    /// </summary>
    public void StartInstruction()
    {
        SceneManager.Instance.OpenInstructionScene();
    }

    /// <summary>
    /// 退出游戏
    /// </summary>
	public void QuitGame()
	{
		Application.Quit ();
	}

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <param name="windows"></param>
    public void OpenWindows(GameObject windows)
    {
        if (windows.name == "Setting")
        {
            Slider[] sliders = windows.GetComponentsInChildren<Slider>();
            sliders[0].value = AudioManager.Instance.currentBGMvolume;
            sliders[1].value = AudioManager.Instance.currentEffectvolume;
            sliders[2].value = GameManager.Instance.AI_Difficulty;
            Toggle toggle = windows.GetComponentInChildren<Toggle>();
            toggle.isOn = AudioManager.Instance.AudioOpen;
        }
        windows.SetActive(true);
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="windows"></param>
    public void CloseWindows(GameObject windows)
    {
        if (windows.name == "Setting")
        {
            GameManager.Instance.WriteConfig("Sound", "BGM_Volume", AudioManager.Instance.currentBGMvolume);
            GameManager.Instance.WriteConfig("Sound", "SoundEffect_Volume", AudioManager.Instance.currentEffectvolume);
            GameManager.Instance.WriteConfig("Sound", "SoundIsOn", AudioManager.Instance.AudioOpen);
            GameManager.Instance.WriteConfig("Game", "AI_Difficulty", GameManager.Instance.AI_Difficulty);
        }
        windows.SetActive(false);
    }

    /// <summary>
    /// 设置音量
    /// </summary>
    /// <param name="volume"></param>
    public void SetSoundVolume(float volume)
    {
        AudioManager.Instance.SetSoundVolume(volume);
    }

    /// <summary>
    /// 设置音效
    /// </summary>
    /// <param name="volume"></param>
    public void SetSoundEffectVolume(float volume)
    {
        AudioManager.Instance.SetSoundEffectVolume(volume);
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="clip"></param>
    public void SetEffectSound(AudioClip clip)
    {
        AudioManager.Instance.PlayVoice(clip);
    }

    /// <summary>
    /// 开启or关闭所有声音
    /// </summary>
    /// <param name="state"></param>
    public void SetAllMucic(bool state)
    {
        AudioManager.Instance.AudioOpen = state;
    }

    /// <summary>
    /// 弹出提示更新的窗口
    /// </summary>
    public void ShowUpdateWindow()
    {
        Debug.Log("Need Update!");
    }

    //插屏广告
    public void ShowAd()
    {
        // 创建插页广告实例，并传递广告ID，错误回调和关闭回调
        interstitialAd = TT.CreateInterstitialAd(tableScreenAdid, errCallback, closeCallback);

        // 加载广告
        interstitialAd.Load();

        // 在广告加载成功后展示广告
        interstitialAd.OnLoad += () =>
        {
            // 广告加载成功，展示广告
            interstitialAd.Show();
        };
    }

    private void errCallback(int errorCode, string errorMessage)
    {
        // 处理广告加载失败的情况
        Debug.Log("观看广告失败，错误码：" + errorCode + " 错误信息：" + errorMessage);

    }

    private void closeCallback()
    {
        MaidianNew.MaidianBackCallback(" ");
        // 处理广告关闭后的回调
        Debug.Log("广告关闭");
        if (interstitialAd != null)
        {
            interstitialAd.Destroy();
        }
        interstitialAd = null;
    }

    private IEnumerator OpenInterstitialAd()
    {
        // 等待指定的时间
        yield return null;

        // 这里执行你想要延迟执行的操作
        UnityEngine.Debug.Log("1帧后执行的操作");

        ShowAd();
        isFirstEnter = false;
    }
}
